﻿using System;
using Lastfm.Services;
using System.Net;

namespace LibraryImport
{
    static class LastfmClient
    {
        private static Session LastfmSession;
        private static string LastfmUsername;

        private const string ApiKey = "591ace67a286f518592adbfa25327f7d";
        private const string ApiSecret = "dca1f0086d6200b6cfbc73dd8fb115c8";

        /// <summary>
        /// Initiaize a new session
        /// </summary>
        public static void Initialize()
        {
            LastfmSession = new Session(ApiKey, ApiSecret);
            LastfmUsername = "";
        }

        /// <summary>
        /// Try to authenticate user
        /// </summary>
        /// <param name="Username">User nickname</param>
        /// <param name="PasswordHash">md5 hash of the password</param>
        /// <returns>Returns null if ok or text of the exception</returns>
        public static string Authenticate(string Username, string PasswordHash)
        {
            try
            {
                LastfmSession.Authenticate(Username, PasswordHash);
                LastfmUsername = Username;
                return null;
            }
            catch (Exception e)
            {
                return e.Message;
            }
        }

        /// <summary>
        /// Imports the artist list to the user's library
        /// </summary>
        /// <param name="ArtistNames">Artist names</param>
        public static string ImportLibrary(string[] ArtistNames)
        {
            Library UserLibrary = new Library(LastfmUsername, LastfmSession);

            for (int i = 0; i < ArtistNames.Length; i++)
            {
                try
                {
                    UserLibrary.AddArtist(new Artist(ArtistNames[i], LastfmSession));
                }
                catch (Exception e)
                {
                    return e.Message;
                }
               
                Frontend.ProgressPercentage = 100 * i / ArtistNames.Length;                
            }

            return null;
        }

        /// <param name="ArtistName">Artist name</param>
        /// <returns>Returns a string, containing an url of Artist image</returns>
        public static string GetImageURL(string ArtistName)
        {
            try
            {
                return new Artist(ArtistName, LastfmSession).GetImageURL();
            }
            catch (WebException)
            {
                return null;
            }
        }

        /// <returns>Returns true if user is authenticated, else - false</returns>
        public static bool isAuthenticated()
        {
            return LastfmSession.Authenticated;
        }

        /// <returns>Returns username</returns>
        public static string GetUsername()
        {
            return LastfmUsername;
        }

        /// <returns>Returns url of user's last.fm page</returns>
        public static string GetUserURL()
        {
            try
            {
                return new User(LastfmUsername, LastfmSession).URL;
            }
            catch (WebException)
            {
                return null;
            }
        }

        /// <returns>Returns url of user's avatar on last.fm page</returns>
        public static string GetUserImageURL()
        {
            try
            {
                return AuthenticatedUser.GetUser(LastfmSession).GetImageURL();
            }
            catch (WebException)
            {
                return null;
            }
        }
    }
}
